package com.synesthesiaconsulting.gwt.resources.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.gwtext.client.data.*;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.grid.Renderer;
import com.gwtext.client.widgets.grid.CellMetadata;

import java.util.HashMap;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 23, 2008
 * Time: 11:17:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class GWTUtils {
    public static final Object[][] STATES_ARRAY = new String[][]{
               new String[]{"AL", "Alabama"},
               new String[]{"AK", "Alaska"},
               new String[]{"AZ", "Arizona"},
               new String[]{"AR", "Arkansas"},
               new String[]{"CA", "California"},
               new String[]{"CO", "Colorado"},
               new String[]{"CN", "Connecticut"},
               new String[]{"DE", "Delaware"},
               new String[]{"DC", "District of Columbia"},
               new String[]{"FL", "Florida"},
               new String[]{"GA", "Georgia"},
               new String[]{"HW", "Hawaii"},
               new String[]{"ID", "Idaho"},
               new String[]{"IL", "Illinois"},
               new String[]{"IN", "Indiana"},
               new String[]{"IA", "Iowa"},
               new String[]{"KS", "Kansas"},
               new String[]{"KY", "Kentucky"},
               new String[]{"LA", "Louisiana"},
               new String[]{"ME", "Maine"},
               new String[]{"MA", "Massachusetts"},
               new String[]{"MD", "Maryland"},
               new String[]{"MI", "Michigan"},
               new String[]{"MN", "Minnesota"},
               new String[]{"MS", "Mississippi"},
               new String[]{"MO", "Missouri"},
               new String[]{"MT", "Montana"},
               new String[]{"NE", "Nebraska"},
               new String[]{"NV", "Nevada"},
               new String[]{"NH", "New Hampshire"},
               new String[]{"NJ", "New Jersey"},
               new String[]{"NM", "New Mexico"},
               new String[]{"NY", "New York"},
               new String[]{"NC", "North Carolina"},
               new String[]{"ND", "North Dakota"},
               new String[]{"OH", "Ohio"},
               new String[]{"OK", "Oklahoma"},
               new String[]{"OR", "Oregon"},
               new String[]{"PA", "Pennsylvania"},
               new String[]{"RH", "Rhode Island"},
               new String[]{"SC", "South Carolina"},
               new String[]{"SD", "South Dakota"},
               new String[]{"TE", "Tennessee"},
               new String[]{"TX", "Texas"},
               new String[]{"UT", "Utah"},
               new String[]{"VE", "Vermont"},
               new String[]{"VA", "Virginia"},
               new String[]{"WA", "Washington"},
               new String[]{"WV", "West Virginia"},
               new String[]{"WI", "Wisconsin"},
               new String[]{"WY", "Wyoming"}
       };
    public static Renderer dateRender = new Renderer() {
        public String render(Object o, CellMetadata cellMetadata, Record record, int i, int i1, Store store) {
            Date myDate = (Date) o;
            return DateTimeFormat.getFormat("MMMM d, yyyy").format(myDate);
        }
    };
    public static FieldDef[] createFieldDefArrayFromHeader(String[] header) {
        FieldDef[] ret = new FieldDef[header.length];
        for (int i = 0; i < ret.length; i++) {
            String[] def = header[i].split("_:_");
            ret[i] = createFieldDef(def[0], def[1], i, (def.length > 2) ? def[2] : "");
        }
        
        return ret;
    }
    public static FieldDef createFieldDef(String type, String name, int col, String format) {
        if (type.equals("integer")) {
            return new IntegerFieldDef(name, col);
        } else if (type.equals("string")) {
            return new StringFieldDef(name, col);
        } else if (type.equals("date")) {
            return new DateFieldDef(name, col, format);
        } else if (type.equals("boolean")) {
            return new BooleanFieldDef(name, col);
        } else {
            return null;
        }
    }
   
    public static String firstModule(String actionName) {
        return actionName.substring(0,actionName.indexOf("."));
    }
    public static String restModule(String actionName) {
        return actionName.substring(actionName.indexOf(".") + 1);
    }
    public static Boolean isLastModule(String actionName) {
        return ((actionName.indexOf('.')) < 1);
    }
    public static String packageToUrl(String p) {
        if (isLastModule(p)) return p + "/" + p + ".jsp";
        else return firstModule(p) + "/" + packageToUrl(restModule(p));
    }
    public static String getUrlWithContext() {
       return GWT.getModuleBaseURL().substring(0, GWT.getModuleBaseURL().indexOf("/gwt"));    
    }

    public static native int getUserId() /*-{
        return $doc.userinfo.userId;
    }-*/;
}
